//
//  SkillGroupVC.m
//  EVETracker
//
//  Created by Scott Guyer on 7/30/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SkillVC.h"
#import "SkillListVC.h"
#import "SkillGroupVC.h"
#import "InventoryItem.h"
#import "Skill.h"
#import "EVEdb.h"
#import "defs.h"


@implementation SkillGroupVC


@synthesize tableView, navVC;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
		listVC = nil;
	}
	return self;
}


- (void)viewDidLoad 
{
	NSLog(@"%s", __FUNCTION__);
	[self.view setAutoresizesSubviews:YES];
	
	[self.view addSubview:tableView];
	
}

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tv
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
	return (groupRows != nil) ? [groupRows count] : 0;
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellAccessoryDisclosureIndicator;
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"skillGroupCell"];
	if (cell == nil)
	{
		CGRect zero = CGRectMake(0,0,0,0);
		cell = [[[UITableViewCell alloc] initWithFrame:zero reuseIdentifier:@"skillGroupCell"] autorelease];
	}
	

	cell.selectedTextColor = [UIColor blackColor];
	cell.selectionStyle = UITableViewCellSelectionStyleGray;
	cell.textColor = [UIColor whiteColor];
	cell.text = (NSString*)[groupRows objectAtIndex:indexPath.row];
	
	return cell;
}


#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if ( listVC == nil )
	{
		listVC = [[SkillListVC alloc] initWithNibName:@"SkillListView" bundle:nil];
		if ( listVC == nil )
		{
			NSLog(@"%s unable to create skills list view", __FUNCTION__);
			return;
		}	
		listVC.navVC = navVC;
	}
	
	
	NSString* groupName = (NSString*)[groupRows objectAtIndex:indexPath.row];
	//listVC.navigationItem.title = groupName;
	
	if ( viewMode == SkillGroupViewModeBrowser )
	{		
		[listVC loadSkillsInGroup:groupName
						   withId:[[groupData objectForKey:groupName] unsignedIntValue]];
	}
	else
	{		
		[listVC loadSkillsInGroup:groupName fromPilot:pilot];
	}

	[listVC viewWillAppear:YES];
	[navVC pushViewController:listVC animated:YES];	
	[listVC viewDidAppear:YES];

	listVC.navigationItem.title = groupName;
	
	[tv deselectRowAtIndexPath:[tv indexPathForSelectedRow] animated:YES];
}


- (void) setBrowserMode
{
	// Load up the groups data
	
	if ( groupData != nil )
		[groupData release];
	
	groupData = [EVEdb getSkillGroups];
	if ( groupData == nil )
	{
		NSLog(@"%s unable to load skill group table");
		return;
	}
	
	if ( groupRows != nil )
		[groupRows release];
	
	groupRows = [groupData allKeys];
	[groupRows retain];
	
	viewMode = SkillGroupViewModeBrowser;
	
	[tableView reloadData];	
}

- (void) setPilotModeWithPilot:(Character*)p 
{
	if ( pilot != nil )
		[pilot release];
	
	pilot = p;
	[pilot retain];
	
	// Build a list of groups that this pilot has skills for
	
	
	NSMutableSet* groupSet = [[NSMutableSet alloc] init];
	
	NSEnumerator *enumerator = [pilot.skills objectEnumerator];
	id value;	
	Skill* s;
	while ((value = [enumerator nextObject])) 
	{
		if ( [value isKindOfClass:[Skill class]] )
		{
			s = (Skill*) value;
			[groupSet addObject:s.groupName];			
		}
	}
	
	groupRows = [groupSet allObjects];
	[groupRows retain];
	[groupSet release];
	
	viewMode = SkillGroupViewModePilot;
	
	[tableView reloadData];	
}


- (void)viewWillAppear:(BOOL)animated
{	
	if ( (groupData == nil) && (groupRows == nil) )
	{
		// We must have been purged by a memory warning; get reset for display
		NSLog(@"aye");
	}
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning 
{
}


- (void) jenny
{
	NSLog(@"%s", __FUNCTION__);
	
	[listVC jenny];
	WIPE( listVC)

	WIPE( groupRows)
	WIPE( groupData)
}

- (void)dealloc {
	NSLog(@"%s 0x%x", __FUNCTION__, self);
	
	WIPE( tableView)
	WIPE( navVC)
	WIPE( groupData)
	WIPE( groupRows)
	WIPE( listVC)
	WIPE( pilot)
	
	[super dealloc];
}


@end
